<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="50%"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
  >
    <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('departmentCode')" prop="code" :error="error.code">
            <el-input v-model="form.code" maxlength="3" show-word-limit :disabled="!createFlag" :placeholder="$t('inputdepartmentCode')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('departmentName')" prop="name" :error="error.name">
            <el-input v-model="form.name" maxlength="10" show-word-limit :placeholder="$t('inputdepartmentName')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" :disabled="!isModified" @click="handleCreate">{{ $t('create') }}</el-button>
      <el-button v-else type="primary" :disabled="!isModified" @click="handleUpdate">{{ $t('update') }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import sheetMixin from '@/mixins/SheetMixin'
import { createDepartmentsApi, updateDepartmentsApi } from '../api/departments.api';

export default {
  mixins: [sheetMixin],
  data() {
    return {
      formRef: 'form',
      createTitle: this.$t('departmentsCreateTitle'),
      updateTitle: this.$t('departmentsUpdateTitle'),
      menuOptions: [],
      actionApi: {
        'create': createDepartmentsApi,
        'update': updateDepartmentsApi
      },
      form: {
        code: undefined,
        name: undefined
      },
      error: {
        code: undefined,
        name: undefined
      },
      rules: {
        code: [{ required: true, message: this.$t('departmentCodeRequired'), trigger: 'blur' }]
      }
    };
  },
  computed: {
    labelWidth() {
      return this.$i18n.locale === 'en' ? '150px' : '80px';
    }
  },
  methods: {
    initialize() {
    },
    handleCreate() {
      this.submitForm('create', this.$t('createSuccess', { title: this.$t('departmentCode'), value: this.form.code }))
    },
    handleUpdate() {
      this.submitForm('update', this.$t('updateSuccess', { title: this.$t('departmentCode'), value: this.form.code }))
    }
  }
};
</script>

<style scoped>
.el-input input, .table-select .el-input__inner {
    width: 220px;
    box-sizing: border-box;
}
::v-deep .el-form-item__label {
    text-align: right;
}
</style>
